import { observable, action } from 'mobx';
import { SearchAdvanced, RouteLayout } from 'weaver-mobile-page';
import * as Api from '../../api';
import { PARAMS } from '../../util';
import PersonListStore from './PersonList';
import UrgentStore from './Urgent';

const initalState = {
  statistics: {},
  report: [],
  path: null,
  adSearch: null,
  votingid: '',
  joinPersonLayout: null,
  notjoinPersonLayout: null,
  answerLayout: null,
  viewLayout: null,
  othersLayout: null,
  optionLayout: null,
  joinViewLayout: null,
  urgentLayout: null,
  urgentPerLaout: null,
  isUrgent: false,
}

export default class ResultStore {
  @observable personListStore = new PersonListStore();
  @observable urgentStore = new UrgentStore();

  @observable joinPersonLayout = initalState.joinPersonLayout; // 已参与
  @observable notjoinPersonLayout = initalState.notjoinPersonLayout; // 未参与
  @observable answerLayout = initalState.answerLayout; // 回答
  @observable viewLayout = initalState.viewLayout; // 查看
  @observable othersLayout = initalState.othersLayout; // 其他
  @observable optionLayout = initalState.optionLayout; // 选项百分比
  @observable joinViewLayout = initalState.joinViewLayout; // 已参与查看
  @observable urgentLayout = initalState.urgentLayout; // 催办
  @observable urgentPerLaout = initalState.urgentPerLaout;

  @observable isUrgent = initalState.isUrgent; // 是否显示催办
  @observable statistics = initalState.statistics;
  @observable report = initalState.report;
  @observable path = initalState.path;
  @observable adSearch = initalState.adSearch;
  @observable votingid = initalState.votingid;

  @action setStoreState = (params) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
  }

  @action inital = () => {
    let paramPath = this.path.split('/result/')[0] + '/result/:votingid';
    this.adSearch = new SearchAdvanced.Store({route: this.path, id: 'adSearch'});
    /* 替换成动态的之后，单个页面不能刷新 */
    this.joinPersonLayout = new RouteLayout.Store({ route: paramPath, id: 'resJoin'});
    this.notjoinPersonLayout = new RouteLayout.Store({ route: paramPath, id: 'resnotJoin'});
    this.answerLayout = new RouteLayout.Store({ route: paramPath, id: 'answerList/:questionid'});
    this.viewLayout = new RouteLayout.Store({ route: paramPath, id: 'viewList/:questionid'});
    this.optionLayout = new RouteLayout.Store({ route: paramPath, id: 'optionView/:questionid/:optionid'});
    this.othersLayout = new RouteLayout.Store({ route: paramPath, id: 'other/:questionid'});
    this.joinViewLayout = new RouteLayout.Store({ route: paramPath, id: 'resJoin/view/:id'});
    this.urgentLayout = new RouteLayout.Store({ route: paramPath, id: 'urgent'});
    this.urgentPerLaout = new RouteLayout.Store({ route: paramPath, id: 'urgent/resnotJoin'});
    this.getAdsearchCondition();
  }

  @action getAdsearchCondition = (params) => {
    this.adSearch && this.adSearch.getData({
      method: 'GET',
      url: '/api/voting/view/condition',
      params: {...PARAMS, ...params}
    })
  }

  @action getRes = () => {
    let params = {votingid: this.votingid};
    let formParams = this.adSearch && this.adSearch.form.formParams;
    formParams = formParams || {};
    params = {...params, ...formParams};
    Api.resultView(params).then(res => {
      this.report = res.datas;
      this.isUrgent = res.votingStatus === '1';
    })
  }

  @action getpartSituation = () => {
    let params = {votingid: this.votingid}
    Api.partSituation(params).then(res => {
      this.statistics = res;
    })
  }

  @action doResultReset = () => {
    
  }
}